<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="userPermissions">
              <a-select @change="numChange" placeholder="请选择用户" v-model="model.name" @select="checkUser">
                <a-select-option v-for="index in templateData" :key="index.userName" :value="index.userName">
                  {{ index.userName }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="工号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="number">
              <a-input v-model="model.number" placeholder="工号" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="权限选择" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="permission">
              <j-dict-select-tag type="list" v-model="model.permission" dictCode="permission" placeholder="请选择权限选择" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'PermissionForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: { number: '', userId: '' },
      templateData: [],
      userPermissions: [],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        userPermissions: {
          required: false, message: '当前用户已存在'
        }
      },
      url: {
        add: '/permission/permission/add',
        edit: '/permission/permission/edit',
        queryById: '/permission/permission/queryById',
        listUser: '/permission/permission/listUser',
        listUserPermission: '/permission/permission/listUserPermission'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  mounted() {
    this.listUser()
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },

  methods: {
    listUser() {
      getAction(this.url.listUser).then(res => {
        if (res.success) {
          this.templateData = res.result
        }
      }).catch(error => {
        this.$message.error('请求错误')
      })
    },

    numChange(value) {
      for (let i = 0; i < this.templateData.length; i++) {
        if (this.templateData[i].userName === value) {
          this.model.number = this.templateData[i].userNum
          this.model.userId = this.templateData[i].id
        }

      }
    },
    checkUser(value) {
      console.log(value)
      getAction(this.url.listUserPermission).then(res => {
        if (res.success) {
          for (let i = 0; i < res.result.length; i++) {
            if (value === res.result[i].name) {
              console.log("if")
              this.validatorRules.userPermissions.required = true
              break
            }
            this.validatorRules.userPermissions.required = false
            // else if(value!==res.result[i].name){
            //   console.log("else if")
            //   this.validatorRules.userPermissions.required = false
            // }
          }
          //this.validatorRules.userPermissions.required = false
        }
      })
    },
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            console.log(this.model)
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    }
  }
}
</script>